//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package javax.vpncrypto;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.ProviderException;
import java.security.SecureRandom;
import java.security.Provider.Service;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Iterator;
import java.util.List;
import sun.security.jca.GetInstance;
import sun.security.jca.GetInstance.Instance;
import sun.security.util.Debug;

public class KeyAgreement {
    private static final Debug debug = Debug.getInstance("jca", "KeyAgreement");
    private static final Debug pdebug = Debug.getInstance("provider", "Provider");
    private static final boolean skipDebug = Debug.isOn("engine=") && !Debug.isOn("keyagreement");
    private Provider provider;
    private javax.vpncrypto.KeyAgreementSpi spi;
    private final String algorithm;
    private Service firstService;
    private Iterator<Service> serviceIterator;
    private final Object lock;
    private static int warnCount = 10;
    private static final int I_NO_PARAMS = 1;
    private static final int I_PARAMS = 2;

    protected KeyAgreement(javax.vpncrypto.KeyAgreementSpi var1, Provider var2, String var3) {
        this.spi = var1;
        this.provider = var2;
        this.algorithm = var3;
        this.lock = null;
    }

    private KeyAgreement(Service var1, Iterator<Service> var2, String var3) {
        this.firstService = var1;
        this.serviceIterator = var2;
        this.algorithm = var3;
        this.lock = new Object();
    }

    public final String getAlgorithm() {
        return this.algorithm;
    }

    public static final KeyAgreement getInstance(String var0) throws NoSuchAlgorithmException {
        List var1 = GetInstance.getServices("KeyAgreement", var0);
        Iterator var2 = var1.iterator();

        Service var3;
        do {
            if (!var2.hasNext()) {
                throw new NoSuchAlgorithmException("Algorithm " + var0 + " not available");
            }

            var3 = (Service)var2.next();
        } while(!javax.vpncrypto.JceSecurity.canUseProvider(var3.getProvider()));

        return new KeyAgreement(var3, var2, var0);
    }

    public static final KeyAgreement getInstance(String var0, String var1) throws NoSuchAlgorithmException, NoSuchProviderException {
        Instance var2 = javax.vpncrypto.JceSecurity.getInstance("KeyAgreement", javax.vpncrypto.KeyAgreementSpi.class, var0, var1);
        return new KeyAgreement((javax.vpncrypto.KeyAgreementSpi)var2.impl, var2.provider, var0);
    }

    public static final KeyAgreement getInstance(String var0, Provider var1) throws NoSuchAlgorithmException {
        Instance var2 = javax.vpncrypto.JceSecurity.getInstance("KeyAgreement", javax.vpncrypto.KeyAgreementSpi.class, var0, var1);
        return new KeyAgreement((javax.vpncrypto.KeyAgreementSpi)var2.impl, var2.provider, var0);
    }

    void chooseFirstProvider() {
        if (this.spi == null) {
            synchronized(this.lock) {
                if (this.spi == null) {
                    if (debug != null) {
                        int var2 = --warnCount;
                        if (var2 >= 0) {
                            debug.println("KeyAgreement.init() not first method called, disabling delayed provider selection");
                            if (var2 == 0) {
                                debug.println("Further warnings of this type will be suppressed");
                            }

                            (new Exception("Call trace")).printStackTrace();
                        }
                    }

                    Exception var8 = null;

                    while(true) {
                        Service var3;
                        do {
                            if (this.firstService == null && !this.serviceIterator.hasNext()) {
                                ProviderException var9 = new ProviderException("Could not construct KeyAgreementSpi instance");
                                if (var8 != null) {
                                    var9.initCause(var8);
                                }

                                throw var9;
                            }

                            if (this.firstService != null) {
                                var3 = this.firstService;
                                this.firstService = null;
                            } else {
                                var3 = (Service)this.serviceIterator.next();
                            }
                        } while(!javax.vpncrypto.JceSecurity.canUseProvider(var3.getProvider()));

                        try {
                            Object var4 = var3.newInstance((Object)null);
                            if (var4 instanceof javax.vpncrypto.KeyAgreementSpi) {
                                this.spi = (javax.vpncrypto.KeyAgreementSpi)var4;
                                this.provider = var3.getProvider();
                                this.firstService = null;
                                this.serviceIterator = null;
                                return;
                            }
                        } catch (Exception var6) {
                            var8 = var6;
                        }
                    }
                }
            }
        }
    }

    private void implInit(javax.vpncrypto.KeyAgreementSpi var1, int var2, Key var3, AlgorithmParameterSpec var4, SecureRandom var5) throws InvalidKeyException, InvalidAlgorithmParameterException {
        if (var2 == 1) {
            var1.engineInit(var3, var5);
        } else {
            var1.engineInit(var3, var4, var5);
        }

    }

    private void chooseProvider(int var1, Key var2, AlgorithmParameterSpec var3, SecureRandom var4) throws InvalidKeyException, InvalidAlgorithmParameterException {
        synchronized(this.lock) {
            if (this.spi != null) {
                this.implInit(this.spi, var1, var2, var3, var4);
            } else {
                Exception var6 = null;

                while(true) {
                    Service var7;
                    do {
                        do {
                            if (this.firstService == null && !this.serviceIterator.hasNext()) {
                                if (var6 instanceof InvalidKeyException) {
                                    throw (InvalidKeyException)var6;
                                }

                                if (var6 instanceof InvalidAlgorithmParameterException) {
                                    throw (InvalidAlgorithmParameterException)var6;
                                }

                                if (var6 instanceof RuntimeException) {
                                    throw (RuntimeException)var6;
                                }

                                String var12 = var2 != null ? var2.getClass().getName() : "(null)";
                                throw new InvalidKeyException("No installed provider supports this key: " + var12, var6);
                            }

                            if (this.firstService != null) {
                                var7 = this.firstService;
                                this.firstService = null;
                            } else {
                                var7 = (Service)this.serviceIterator.next();
                            }
                        } while(!var7.supportsParameter(var2));
                    } while(!javax.vpncrypto.JceSecurity.canUseProvider(var7.getProvider()));

                    try {
                        javax.vpncrypto.KeyAgreementSpi var8 = (KeyAgreementSpi)var7.newInstance((Object)null);
                        this.implInit(var8, var1, var2, var3, var4);
                        this.provider = var7.getProvider();
                        this.spi = var8;
                        this.firstService = null;
                        this.serviceIterator = null;
                        return;
                    } catch (Exception var10) {
                        if (var6 == null) {
                            var6 = var10;
                        }
                    }
                }
            }
        }
    }

    public final Provider getProvider() {
        this.chooseFirstProvider();
        return this.provider;
    }

    public final void init(Key var1) throws InvalidKeyException {
        this.init(var1, javax.vpncrypto.JceSecurity.RANDOM);
    }

    public final void init(Key var1, SecureRandom var2) throws InvalidKeyException {
        if (this.spi != null) {
            this.spi.engineInit(var1, var2);
        } else {
            try {
                this.chooseProvider(1, var1, (AlgorithmParameterSpec)null, var2);
            } catch (InvalidAlgorithmParameterException var4) {
                throw new InvalidKeyException(var4);
            }
        }

        if (!skipDebug && pdebug != null) {
            pdebug.println("KeyAgreement." + this.algorithm + " algorithm from: " + this.provider.getName());
        }

    }

    public final void init(Key var1, AlgorithmParameterSpec var2) throws InvalidKeyException, InvalidAlgorithmParameterException {
        this.init(var1, var2, javax.vpncrypto.JceSecurity.RANDOM);
    }

    public final void init(Key var1, AlgorithmParameterSpec var2, SecureRandom var3) throws InvalidKeyException, InvalidAlgorithmParameterException {
        if (this.spi != null) {
            this.spi.engineInit(var1, var2, var3);
        } else {
            this.chooseProvider(2, var1, var2, var3);
        }

        if (!skipDebug && pdebug != null) {
            pdebug.println("KeyAgreement." + this.algorithm + " algorithm from: " + this.provider.getName());
        }

    }

    public final Key doPhase(Key var1, boolean var2) throws InvalidKeyException, IllegalStateException {
        this.chooseFirstProvider();
        return this.spi.engineDoPhase(var1, var2);
    }

    public final byte[] generateSecret() throws IllegalStateException {
        this.chooseFirstProvider();
        return this.spi.engineGenerateSecret();
    }

    public final int generateSecret(byte[] var1, int var2) throws IllegalStateException, ShortBufferException {
        this.chooseFirstProvider();
        return this.spi.engineGenerateSecret(var1, var2);
    }

    public final SecretKey generateSecret(String var1) throws IllegalStateException, NoSuchAlgorithmException, InvalidKeyException {
        this.chooseFirstProvider();
        return this.spi.engineGenerateSecret(var1);
    }
}
